﻿using System.Collections.Generic;
using MSScriptControl;

namespace ReguLiteCM
{
	/// <summary>
	/// 
	/// </summary>
	class PerlRegexStrategy : IRegexStrategy
	{
		private string _expression;
		readonly private string _input;

		/// <summary>
		/// 
		/// </summary>
		public bool Global{get;set;}
		
		/// <summary>
		/// 
		/// </summary>
		public bool IgnoreCase{get;set;}
		
		/// <summary>
		/// 
		/// </summary>
		public bool MultiLine{get;set;}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="expression"></param>
		/// <param name="input"></param>
		public PerlRegexStrategy(string expression, string input)
		{
			_expression = expression;
			_input = input;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public IEnumerable<string> FindMatches()
		{
			// NOTE: to compile this, I had to: References -> MSScriptControl -> Properties -> Embed Interop Types = false
			// ref: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/f362ce4b-c273-4ab4-970e-d11b31033ade

			ScriptControlClass vm = new ScriptControlClass();
			vm.Language = @"PerlScript";

			string script = string.Format("$match =~ m/{0}/{1}", _expression, IgnoreCase ? "i" : string.Empty);
			string matches = (string)vm.Eval(script);

			IList<string> results = new List<string>();
			results.Add(matches);

			return results;
		}
	}
}